<?php

namespace App\Http\Controllers\Home\Hr;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Home\Users as UsersModel;
use App\Models\Home\Wages as WagesModel;
use Carbon\Carbon;
use Wages;
use Users;
use Frame;
use Tools;
use Auth;
use Illuminate\Support\Facades\DB;

class SalaryController extends Controller
{
    protected $passField = [
        'assign','month','rank','salary','kpi_result','kpi_bonus','kpi_money','add_money1','add_money2','add_money3','add_money4','add_money5','add_money6','add_money7','add_money8','add_money9','add_money10','add_money11','add_money12','add_option1','add_money13','add_money14','cut_option1','cut_option2','cut_option3','cut_money1','cut_option4','cut_money2','cut_option5','cut_money3','cut_option6','cut_money4','cut_money5','cut_money6','cut_money7','cut_money8','cut_money9'
    ];

    public function salary(Request $request)
    {
        $opt = [];
        $uid = 0;

        if($request->isMethod('post')){

            if ($uid = $request->post('uid')){

                $opt = array_merge($opt,['assign'=>$uid]);

            }

        }

        $in = [];
        $user_in = [];

        if (!Auth::user()->hasRole('admin')){

            $user = Auth::user()->load('frame');

            $member = Frame::getMember($user->frame->id);

            $member[] = Auth::id();

            $in = ['assign',$member];
            $user_in = ['id',$member];
        }
        $opt['status'] = ['!=',-1];

        $between = Tools::getBetween(false,'Y-m');

        $page = $request->input('page');

        $page_info = Tools::page_helper($page,config('base.home_limit.base_limit.render_limit'),config('base.home_limit.base_limit.limit'));

        $list = Wages::getList($between,$page_info['skip'],$page_info['limit'],$opt,$in);

        $count = Wages::getCount($between,$opt,$in);

        $num = intval(ceil($count/config('base.home_limit.base_limit.limit')));

        foreach ($between as $k => $item){

            $between[$k] = date('Y-m',strtotime($item->toDateTimeString()));

        }

        $user = Users::getUserList([],$user_in);
        $admin = Db::table('frame')->where('level',1)->value('uid');
        return view('Home.User.Hr.Salary.salary',compact('list','user','uid','page','num','between','admin'));
    }

    public function salary_add(Request $request){
        if($request->isMethod('post')){
            //验证
            $this->validate(request(), [
                'month' => 'required|string',
                'rank' => 'required|string',
                'salary' => 'required|string',
                'assign' => 'required|string',
                'kpi_result' => 'nullable|string',
                'kpi_bonus' => 'nullable|string',
                'kpi_money' => 'nullable|string',
                'add_money1' => 'nullable|string',
                'add_money2' => 'nullable|string',
                'add_money3' => 'nullable|string',
                'add_money4' => 'nullable|string',
                'add_money5' => 'nullable|string',
                'add_money6' => 'nullable|string',
                'add_money7' => 'nullable|string',
                'add_money8' => 'nullable|string',
                'add_money9' => 'nullable|string',
                'add_money10' => 'nullable|string',
                'add_money11' => 'nullable|string',
                'add_money12' => 'nullable|string',
                'add_option1' => 'nullable|string',
                'add_money13' => 'nullable|string',
                'add_money14' => 'nullable|string',
                'cut_option1' => 'nullable|string',
                'cut_option2' => 'nullable|string',
                'cut_option3' => 'nullable|string',
                'cut_money1' => 'nullable|string',
                'cut_option4' => 'nullable|string',
                'cut_money2' => 'nullable|string',
                'cut_option5' => 'nullable|string',
                'cut_money3' => 'nullable|string',
                'cut_option6' => 'nullable|string',
                'cut_money4' => 'nullable|string',
                'cut_money5' => 'nullable|string',
                'cut_money6' => 'nullable|string',
                'cut_money7' => 'nullable|string',
                'cut_money8' => 'nullable|string',
                'cut_money9' => 'nullable|string'
            ],[],[
                'month' => '薪资月份',
                'rank' => '职级',
                'salary' => '薪水',
                'assign' => '归属者',
                'kpi_result' => 'KPI结果',
                'kpi_bonus' => 'KPI奖金',
                'kpi_money' => 'KPI提成',
                'add_money1' => '加班费',
                'add_money2' => '推荐奖金',
                'add_money3' => '节庆礼金',
                'add_money4' => '生日礼金',
                'add_money5' => '房补',
                'add_money6' => '餐补',
                'add_money7' => '话费补贴',
                'add_money8' => '入职机票',
                'add_money9' => '入职签证',
                'add_money10' => '年假折现',
                'add_money11' => '年度机票',
                'add_money12' => '年度医补',
                'add_option1' => '13薪基数',
                'add_money13' => '年度13薪',
                'add_money14' => '其他补发',
                'cut_option1' => '总出勤数',
                'cut_option2' => '实际出勤',
                'cut_option3' => '迟到次数',
                'cut_money1' => '迟到扣款',
                'cut_option4' => '病假天数',
                'cut_money2' => '病假扣款',
                'cut_option5' => '事假天数',
                'cut_money3' => '事假扣款',
                'cut_option6' => '旷工天数',
                'cut_money4' => '旷工扣款',
                'cut_money5' => '房租扣款',
                'cut_money6' => '水电费',
                'cut_money7' => '网费',
                'cut_money8' => '五险一金',
                'cut_money9' => '其他扣款'
            ]);

            //数据库操作
            $data = [];
            foreach ($request->all() as $key => $val){
                if (!is_numeric($val)){	$val = clean($val);}
                if(in_array($key,$this->passField) && $val){
                    $val = trim($val);
                    if ($key == 'month'){
                        $val = Carbon::parse($val);
                    }
                    $data[$key] = $val;
                }
            }

            $user = Users::find($data['assign']);

            if($user && ($user->rank != $data['rank'] || $user->salary != $data['salary'])){
                $up_salary_data['assign'] = $data['assign'];
                $up_salary_data['before_rank'] = $user->rank;
                $up_salary_data['before_salary'] = $user->salary;
                $up_salary_data['now_rank'] = $data['rank'];
                $up_salary_data['now_salary'] = $data['salary'];
                $user->rank = $data['rank'];
                $user->salary = $data['salary'];
                $user->save();
                $user->up_salary()->create($up_salary_data);
            }

            WagesModel::create($data);

            return ['code'=>1];
            //返回结果
        }

        $user = Users::getUserList();

        return view('Home.User.Hr.Salary.add',compact('user'));
    }

    public function salary_edit(WagesModel $obj){
        $obj->load(['user']);

        $rank = $obj->rank = $obj->user?$obj->user->rank:'';

        $salary = $obj->salary = $obj->user?intval($obj->user->salary):0;

        if(request() ->isMethod('post')){

            //验证
            $this->validate(request(), [
                'month' => 'nullable|string',
                'rank' => 'nullable|string',
                'salary' => 'nullable|string',
                'kpi_result' => 'nullable|string',
                'kpi_bonus' => 'nullable|string',
                'kpi_money' => 'nullable|string',
                'add_money1' => 'nullable|string',
                'add_money2' => 'nullable|string',
                'add_money3' => 'nullable|string',
                'add_money4' => 'nullable|string',
                'add_money5' => 'nullable|string',
                'add_money6' => 'nullable|string',
                'add_money7' => 'nullable|string',
                'add_money8' => 'nullable|string',
                'add_money9' => 'nullable|string',
                'add_money10' => 'nullable|string',
                'add_money11' => 'nullable|string',
                'add_money12' => 'nullable|string',
                'add_option1' => 'nullable|string',
                'add_money13' => 'nullable|string',
                'add_money14' => 'nullable|string',
                'cut_option1' => 'nullable|string',
                'cut_option2' => 'nullable|string',
                'cut_option3' => 'nullable|string',
                'cut_money1' => 'nullable|string',
                'cut_option4' => 'nullable|string',
                'cut_money2' => 'nullable|string',
                'cut_option5' => 'nullable|string',
                'cut_money3' => 'nullable|string',
                'cut_option6' => 'nullable|string',
                'cut_money4' => 'nullable|string',
                'cut_money5' => 'nullable|string',
                'cut_money6' => 'nullable|string',
                'cut_money7' => 'nullable|string',
                'cut_money8' => 'nullable|string',
                'cut_money9' => 'nullable|string'
            ],[],[
                'month' => '薪资月份',
                'rank' => '职级',
                'salary' => '薪水',
                'kpi_result' => 'KPI结果',
                'kpi_bonus' => 'KPI奖金',
                'kpi_money' => 'KPI提成',
                'add_money1' => '加班费',
                'add_money2' => '推荐奖金',
                'add_money3' => '节庆礼金',
                'add_money4' => '生日礼金',
                'add_money5' => '房补',
                'add_money6' => '餐补',
                'add_money7' => '话费补贴',
                'add_money8' => '入职机票',
                'add_money9' => '入职签证',
                'add_money10' => '年假折现',
                'add_money11' => '年度机票',
                'add_money12' => '年度医补',
                'add_option1' => '13薪基数',
                'add_money13' => '年度13薪',
                'add_money14' => '其他补发',
                'cut_option1' => '总出勤数',
                'cut_option2' => '实际出勤',
                'cut_option3' => '迟到次数',
                'cut_money1' => '迟到扣款',
                'cut_option4' => '病假天数',
                'cut_money2' => '病假扣款',
                'cut_option5' => '事假天数',
                'cut_money3' => '事假扣款',
                'cut_option6' => '旷工天数',
                'cut_money4' => '旷工扣款',
                'cut_money5' => '房租扣款',
                'cut_money6' => '水电费',
                'cut_money7' => '网费',
                'cut_money8' => '五险一金',
                'cut_money9' => '其他扣款'
            ]);

            //数据库操作
            foreach (request()->all() as $key => $val){
                if (!is_numeric($val)){	$val = clean($val);}
                if(in_array($key,$this->passField) && $val){
                    $val = trim($val);
                    if ($key == 'month'){
                        $val = Carbon::parse($val);
                    }
                    $obj->$key = $val;
                }
            }

            if($obj->rank != $rank || $obj->salary != $salary){
                $obj->user->rank = $obj->rank;
                $obj->user->salary = $obj->salary;
                $obj->user->save();
                $up_salary_data['assign'] = $obj->assign;
                $up_salary_data['before_rank'] = $rank;
                $up_salary_data['before_salary'] = $salary;
                $up_salary_data['now_rank'] = $obj->rank;
                $up_salary_data['now_salary'] = $obj->salary;
                $obj->user->up_salary()->create($up_salary_data);
            }

            $obj->save();

            //返回结果
            return ['code'=>1];
        }

        $user = Users::getUserList();

        $obj->month = date('Y-m',strtotime($obj->month));

        return view('Home.User.Hr.Salary.edit',compact('obj','user'));
    }
    /*删除薪酬*/
    public function salary_del(WagesModel $obj){
        if(Auth::user()->hasAnyRole(['admin']) || Auth::id()==$obj->uid){

            $obj->status = '-1';

            $res = $obj->save();

            if ($res){

                $rerurn = ['code'=>1,'msg'=>'OK'];

            }else{

                $rerurn = ['code'=>0,'msg'=>'服务器错误'];
            }
        }else{

            $rerurn = ['code'=>0,'msg'=>'没有权限'];
        }

        return $rerurn;
    }

}